{% extends "personal_center/base.html" %}

{% block page_link %}
    <link rel="stylesheet" href="../../static/system_setup/css/style.css">
{% endblock %}


{% block style %}
    <style>
        .add_class {
            background: #1E90FF;
        }

        tr {
            height: 35px;
        }

        th {
            text-align: center;
        }

        td:first-child, th:first-child {
            position: sticky;
            left: 0; /* 首行永远固定在左侧 */
            z-index: 1;
        }

        thead tr th {
            position: sticky;
            top: 0; /* 列首永远固定在头部  */
            background-color: #E8E8E8;
        }

        th:first-child {
            z-index: 2;
        }

        #tb_1 tr:hover {
            background: #cfcfcf;
        }

        .file {
            position: relative;
            display: inline-block;
            background: #D0EEFF;
            border: 1px solid #99D3F5;
            border-radius: 4px;
            padding: 4px 12px;
            overflow: hidden;
            color: #1E88C7;
            text-decoration: none;
            text-indent: 0;
            line-height: 20px;
        }

        .file input {
            position: absolute;
            font-size: 100px;
            right: 0;
            top: 0;
            opacity: 0;
        }

        .file:hover {
            background: #AADFFD;
            border-color: #78C3F3;
            color: #004974;
            text-decoration: none;
        }

        .search {
            width: 300px;
            display: flex;
            float: right;
            margin: 10px 0;
            margin-right: 1%;
            /*border: 1px solid red;*/
        }

        .search input {
            vertical-align: middle;
            float: left;
        / / 左浮动 flex: 4;
            height: 30px;
            width: 230px;
            outline: none;
            border: 1px solid #0070c1;
            box-sizing: border-box;
        / / 盒子模型，怪异IE盒子模型，width = content + border * 2 + padding * 2 padding-left: 10 px;
            border-bottom-left-radius: 3px;
            border-top-left-radius: 3px;
        }

        .search button {
            float: right;
            flex: 1;
            height: 30px;
            width: 70px;
            line-height: 30px;
            background-color: #0070c1;
            color: white;
            border-style: none;
            outline: none;
            border-bottom-right-radius: 3px;
            border-top-right-radius: 3px;
        }

        .search button i {
            font-style: normal;
        }

        .search button:hover {
            font-size: 16px;
        }

        .check_anniu {
            cursor: pointer;
        }

        #main_table tr td {
            border-right: #cfcfcf solid 0.5px;
            border-left: #cfcfcf solid 0.5px;
            border-bottom: #cfcfcf solid 0.5px;
            height: 35px;
            line-height: 35px;
        }
    </style>
{% endblock %}


{% block right_content %}
    <div class="headline">
        <div style="width: 15%;height: 100%;line-height: 40px;float: left;padding-left: 1%;">
            <a href="#" style="color: #0b0b0b;">
                <i class="iconfont icon-faqi"></i>
                <span> 印章刻制</span>
            </a>
        </div>
        <div style="height: 100%;line-height: 40px;float: right;text-align: right;padding-right: 15px;">
            <button id="add_station" type="button"
                    style="width: 100px;height: 30px;margin-top: 5px;border-radius: 3px;border: #1E90FF solid 1px;background-color: #1E90FF;color: white;line-height: 25px;"
                    onclick="save_data()">
                保存
            </button>
        </div>
        <div style="width: 7%;text-align: center;height: 100%;line-height: 40px;float: right;">
            <button id="add_station" type="button"
                    style="width: 100px;height: 30px;margin-top: 5px;border-radius: 3px;border: #1E90FF solid 1px;background-color: #1E90FF;color: white;line-height: 30px;"
                    onclick="ExportCar()">
                提交
            </button>
        </div>
    </div>
    <hr>

    <div style="width: 100%;height: 93%;overflow: auto;">
        <div id="menu_from"
             style="width: 100%;height: 100%;color: #2C3E50;display: inline-block;vertical-align: top;background: white;">
            <div style="width: 95%;height: 90%;display: inline-block;vertical-align: center;margin-left: 2.5%;margin-top: 15px;">
                <div style="width: 95%;margin-left: 2.5%;">
                    <table id="main_table" style="width: 100%;">
                        <tr>
                            <td colspan="4"
                                style="width: 100%;border: #cfcfcf solid 0.5px;text-align: center;height: 35px;line-height: 35px;">
                                <h3 id="form_name" style="text-align: center;">印章刻制申请单</h3>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4"
                                style="width: 100%;margin-left: 2.5%;height: 40px;padding-left: 0.5%;line-height: 40px;background: #cfcfcf;border: #cfcfcf solid 0.5px;">
                                申请信息
                            </td>
                        </tr>
                        <tr>
                            <td style="text-align: right;padding-right: 10px;width: 15%;">申 请&nbsp;&nbsp;人</td>
                            <td style="text-align: left;padding-left: 10px;width: 30%">{{ obj.apply_user.real_name }}</td>
                            <td style="text-align: right;padding-right: 10px;width: 15%;">申 请 部 门</td>
                            <td style="text-align: left;padding-left: 10px;width: 40%;">{{ obj.company.name }}</td>
                        </tr>
                        <tr>
                            <td style="text-align: right;padding-right: 10px;">申请日期</td>
                            <td style="text-align: left;padding-left: 10px;"
                                id="apply_time">{{ obj.create_time | date:"Y-m-d H:i" }}</td>
                            <td style="text-align: right;padding-right: 10px;">拟刻章名称</td>
                            <td style="text-align: left;"><input id="seal_name" type="text"
                                                                 style="outline: none; width: 100%; height: 100%;border: none;padding-left: 10px"
                                                                 value="{{ obj.plan_seal_name }}" autocomplete="off">
                            </td>
                        </tr>
                        <tr id="add_sa">
                            <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: right;padding-right: 10px;">
                                刻章状况
                            </td>
                            {% if obj.making_number == 1 %}
                                <td id="sasa" style="padding-left: 5px;" colspan="3">
                                    <select name="" id="select_value"
                                            style="width: 100%; border: none; outline: none; line-height: 35px;cursor: pointer"
                                            onchange="deal_more('select_value')">
                                        <option value="once" style="cursor: pointer" selected>初次</option>
                                        <option value="more" style="cursor: pointer">复次</option>
                                    </select>
                                </td>
                            {% else %}
                                <td id="sasa" style="padding-left: 5px;">
                                    <select name="" id="select_value"
                                            style="width: 100%; border: none; outline: none; line-height: 35px;cursor: pointer"
                                            onchange="deal_more('select_value')">
                                        <option value="once" style="cursor: pointer">初次</option>
                                        <option value="more" style="cursor: pointer" selected>复次</option>
                                    </select>
                                </td>
                                <td style="text-align: right;padding-right: 10px;" class='td_asd'>
                                    刻 制 次 数
                                </td>
                                <td style="text-align: left;padding-left: 10px;" class='td_asd'>
                                    <input id="input_number" type="number"
                                           style="outline: none; width: 100%; height: 100%;border: none" value="{{ obj.making_number }}"></td>
                            {% endif %}
                        </tr>
                        <tr>
                            <td style="text-align: right;padding-right: 10px;">申请刻制原因</td>
                            <td colspan="3" style="padding-left: 10px">
                                <div id="seal_reason" contenteditable="true" style="outline: none; width:100%;white-space:normal;word-wrap:break-word;word-break:break-all;" >{{ obj.reason }}</div>
                            </td>
                        </tr>
                    </table>
                </div>
                <div style="width: 95%;margin-left: 2.5%;">
                    <div style="width: 100%;height: 40px;line-height: 40px;background-color: #dfdfdf;color: #1E90FF;padding-left: 1%;border: #cccccc solid 1px;border-top: none;border-bottom: none;padding-right: 1%;">
                        上传附件
                        <a href="javascript:;" class="file" style="vertical-align: top;margin-top: 5px;float: right;">
                            上传文件
                            <input type="file" name="party_main_body" id="party_main_body"
                                   onchange="upload_accessory('file1', 'party_main', 'party_main_body')">
                        </a>
                    </div>
                    <table style="width: 100%;">
                        <thead>
                        <tr>
                            <td style="width: 5%;border: #cccccc solid 1px;text-align: center;">序号</td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">
                                文件名称
                            </td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">
                                文件大小
                            </td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">
                                上传时间
                            </td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">
                                上传人员
                            </td>
                            <td style="width: 15%;border: #cccccc solid 1px;border-left: none;text-align: center;">操作
                            </td>
                        </tr>
                        </thead>
                        <tbody style="overflow: auto;" id="party_main">
                        {% for record in file_list %}
                            <tr>
                                <td style="width: 5%;border: #cccccc solid 1px;text-align: center;">{{ forloop.counter }}</td>
                                <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">{{ record.file_name }}</td>
                                <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">{{ record.file_size }}</td>
                                <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">{{ record.upload_time }}</td>
                                <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">{{ record.uploader }}</td>
                                <td style="width: 15%;border: #cccccc solid 1px;border-left: none;text-align: center;">
                                    <a target='_blank' href="{{ record.file_path }}">查看</a> | <a style="color: red;"
                                                                                                 href='#'
                                                                                                 onclick='RemoveTr(this, "party_main", {{ record.id }})'>删除</a>
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block script %}
    <script src="../../static/system_setup/creat_from/js/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="../../static/finance/js/tooltip.js"></script>
    <script>
        window.onload = function () {
            document.getElementById("loading").style.display = "none";
            document.getElementById("personage").className = "add_class";
            document.getElementById("sponsor").className = "add-nav-active";
        };
        const uuid = '{{ obj.fj_number | safe }}'

        function deal_more(id) {
            var value = $("#" + id).val();
            console.log(value)
            if (value === "more") {
                var html = `<td style="text-align: right;padding-right: 10px;" class='td_asd'>
                                请填写次数</td>
                            <td style="text-align: left;padding-left: 10px;" class='td_asd'>
                                <input id="input_number" type="number"
                                       style="outline: none; width: 100%; height: 100%;border: none"></td>`
                $("#add_sa").append(html)
                $("#sasa").removeAttr("colspan")
            } else {
                $(".td_asd").remove()
                $("#sasa").attr('colspan', '3')
            }
        }

        function ExportCar() {
            var apply_time = $("#apply_time").text()
            var seal_name = $('#seal_name').val()
            var making_number = $("#input_number").val()
            if (!making_number) {
                making_number = 1
            }
            var reason = $('#seal_reason').text()
            if (!seal_name) {
                alert("拟印章名称不能为空！")
                return false
            }
            if (!making_number) {
                alert("刻章状况不能为空！选择“复次”必须输入次数。")
                return false
            }
            if (!reason) {
                alert("申请原因不能为空！")
                return false
            }
            document.getElementById("loading").style.display = "inline-block";
            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/finance/seal_making_apply_view", true);
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            xhr.send("apply_time=" + apply_time + "&id=" + String({{obj.id}}) + "&seal_name=" + seal_name + "&making_number=" + making_number + "&reason=" + reason + "&uuid=" + String(uuid));
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        if (JsonDate["code"] === 200) {
                            alert(JsonDate["message"])
                            window.location.href = "/finance/seal_making_list_view"
                        } else {
                            alert(JsonDate["message"])
                        }
                    }
                }
            }
        }

        function save_data() {
            var apply_time = $("#apply_time").text()
            var seal_name = $('#seal_name').val()
            var making_number = $("#input_number").val()
            if (!making_number) {
                making_number = 1
            }
            var reason = $('#seal_reason').text()
            document.getElementById("loading").style.display = "inline-block";
            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/finance/seal_making_draft_view", true);
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            xhr.send("apply_time=" + apply_time + "&id=" + String({{obj.id}}) + "&seal_name=" + seal_name + "&making_number=" + making_number + "&reason=" + reason + "&uuid=" + String(uuid));
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        if (JsonDate["code"] === 200) {
                            alert(JsonDate["message"])
                            window.location.href = "/finance/seal_making_list_view"
                        } else {
                            alert(JsonDate["message"])
                        }
                    }
                }
            }
        }

        // 上传附件
        function upload_accessory(file_type, tbody_id, accessory_type) {
            document.getElementById("loading").style.display = "inline-block";
            var form = new FormData();
            var files = document.getElementById(accessory_type).files[0];
            form.append(file_type, files);
            form.append('number', uuid)
            form.append('source', 'lcyx')
            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/administrative/topics_attachments_upload", true);
            xhr.send(form);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        console.log(JsonDate);
                        creat_table(JsonDate, tbody_id)
                    }
                }
            };
        }

        // 创建附件列表
        function creat_table(JsonData, tbody_id) {
            var t_table = document.getElementById(tbody_id);
            var local_host = window.location.host;
            if (Array.isArray(JsonData["pdf_path"])) {
                for (var i = 0; i < JsonData["pdf_path"].length; i++) {
                    var tr = document.createElement("tr");
                    var td1 = document.createElement("td");
                    var td2 = document.createElement("td");
                    var td3 = document.createElement("td");
                    var td4 = document.createElement("td");
                    var td5 = document.createElement("td");
                    var td6 = document.createElement("td");
                    td1.innerHTML = t_table.children.length + 1;
                    td1.style.textAlign = "center";
                    td1.style.border = "#cccccc solid 1px";
                    td1.style.borderTop = "none";
                    td2.innerHTML = JsonData["file_name"];
                    td2.style.paddingLeft = "1%";
                    td2.style.border = "#cccccc solid 1px";
                    td2.style.borderLeft = "none";
                    td2.style.borderTop = "none";
                    td3.innerHTML = JsonData["file_size"];
                    td3.style.textAlign = "center";
                    td3.style.border = "#cccccc solid 1px";
                    td3.style.borderLeft = "none";
                    td3.style.borderTop = "none";
                    td4.innerHTML = JsonData["upload_time"];
                    td4.style.textAlign = "center";
                    td4.style.border = "#cccccc solid 1px";
                    td4.style.borderLeft = "none";
                    td4.style.borderTop = "none";
                    td5.innerHTML = JsonData["uploader"];
                    td5.style.textAlign = "center";
                    td5.style.border = "#cccccc solid 1px";
                    td5.style.borderLeft = "none";
                    td5.style.borderTop = "none";
                    td6.innerHTML = "<a target='_blank' href='" + JsonData["pdf_path"][i]["pdf_path"] + "'>查看</a> | <a style='color: red;' href='#' onclick='RemoveTr(this, \"" + tbody_id + "\", \"" + JsonData["pdf_path"][i]["id"] + "\")'>删除</a>";
                    td6.style.textAlign = "center";
                    td6.style.border = "#cccccc solid 1px";
                    td6.style.borderLeft = "none";
                    td6.style.borderTop = "none";
                    tr.appendChild(td1);
                    tr.appendChild(td2);
                    tr.appendChild(td3);
                    tr.appendChild(td4);
                    tr.appendChild(td5);
                    tr.appendChild(td6);
                    t_table.appendChild(tr);
                }
                if (JsonData["pdf_path"].length > 1) {
                    document.getElementById("del_adjunct").style.display = 'block';
                }
            } else {
                var tr = document.createElement("tr");
                var td1 = document.createElement("td");
                var td2 = document.createElement("td");
                var td3 = document.createElement("td");
                var td4 = document.createElement("td");
                var td5 = document.createElement("td");
                var td6 = document.createElement("td");
                td1.innerHTML = t_table.children.length + 1;
                td1.style.textAlign = "center";
                td1.style.border = "#cccccc solid 1px";
                td1.style.borderTop = "none";
                td2.innerHTML = JsonData["file_name"];
                td2.style.paddingLeft = "1%";
                td2.style.border = "#cccccc solid 1px";
                td2.style.borderLeft = "none";
                td2.style.borderTop = "none";
                td3.innerHTML = JsonData["file_size"];
                td3.style.textAlign = "center";
                td3.style.border = "#cccccc solid 1px";
                td3.style.borderLeft = "none";
                td3.style.borderTop = "none";
                td4.innerHTML = JsonData["upload_time"];
                td4.style.textAlign = "center";
                td4.style.border = "#cccccc solid 1px";
                td4.style.borderLeft = "none";
                td4.style.borderTop = "none";
                td5.innerHTML = JsonData["uploader"];
                td5.style.textAlign = "center";
                td5.style.border = "#cccccc solid 1px";
                td5.style.borderLeft = "none";
                td5.style.borderTop = "none";
                td6.innerHTML = "<a target='_blank' href='" + JsonData["pdf_path"] + "'>查看</a> | <a style='color: red;' href='#' onclick='RemoveTr(this, \"" + tbody_id + "\", \"" + JsonData["id"] + "\")'>删除</a>";
                td6.style.textAlign = "center";
                td6.style.border = "#cccccc solid 1px";
                td6.style.borderLeft = "none";
                td6.style.borderTop = "none";
                tr.appendChild(td1);
                tr.appendChild(td2);
                tr.appendChild(td3);
                tr.appendChild(td4);
                tr.appendChild(td5);
                tr.appendChild(td6);
                t_table.appendChild(tr);
            }
        }

        function RemoveTr(_this, tbody_id, wj_id) {
            var table = document.getElementById(tbody_id);
            var trNode = $(_this).parent().parent();
            trNode.remove();

            for (var i = 0; i < table.children.length; i++) {
                table.children[i].children[0].innerHTML = i + 1;
            }

            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/administrative/delete_attachments_view", true);
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            xhr.send("id=" + String(wj_id));
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        console.log(JsonDate);
                        alert("删除成功！")
                    }
                }
            };
        }
    </script>
{% endblock %}
